package model.data.container;

import java.util.ArrayList;
import java.util.List;

import model.OrderLine;
import model.data.DuplicateRecordException;
import model.data.OrderLineStore;
import model.data.RecordNotFoundException;

public class OrderLineContainer implements OrderLineStore {
	/* List of order lines */
	private ArrayList<OrderLine> orderLines;
	
	/** Constructs a new order line container */
	public OrderLineContainer() {
		 this.orderLines = new ArrayList<OrderLine>();
	}
	
	/** Writes an order line to the container */
	@Override
	public void write(OrderLine orderLine) throws DuplicateRecordException {
		if (this.getById(orderLine.getId()) == null) {
			this.orderLines.add(orderLine);
		}
		else
		{
			throw new DuplicateRecordException();
		}
	}
	
	/** Updates an existing order line in the container */
	@Override
	public void update (OrderLine orderLine) {
		// TODO update logic
		// currently impossible due to referencing issue
	}
	
	/** Deletes an existing customer from the container */
	@Override
	public void delete(OrderLine orderLine) throws RecordNotFoundException {
		if (this.getById(orderLine.getId()) != null) {
			this.orderLines.remove(orderLine);
		}
		else
		{
			throw new RecordNotFoundException();
		}
	}
	
	/** Returns the number of items in the container */
	@Override
	public int getCount() {
		return this.orderLines.size();
	}
	
	/** Returns a customer from the container with a specific id */
	@Override
	public List<OrderLine> getById(long id) {
		ArrayList<OrderLine> orderLines = new ArrayList<OrderLine>();
		for (OrderLine orderLine : this.orderLines){
			if(orderLine.getId() == id){
				orderLines.add(orderLine);
			}
		}
		return orderLines;
	}
}